package com.faceye.test.util;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

@Slf4j
public class MD5Util {
    static MessageDigest md = null;
    static{
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            log.error(">>MD5 install errlr:",e);
        }
    }

    /**
     * 货取字符串指纹
     * @param content
     * @return
     */
    public static String getMD5Sign(String content){
        String res ="";
        if(md==null || StringUtils.isEmpty(content)){
            log.warn(">>MD is null or content is empty");
        }else{
            md.reset();
            res=new String(Hex.encodeHex(md.digest(content.getBytes())));
        }
        return res;
    }
}
